/*
 * Created on 2005-11-17
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package monster.comm.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Vector;
import org.apache.commons.beanutils.BeanUtilsBean;

/**
 * @author jiang
 *这个是BeanUtilsBean的扩展类，
* 实现不同bean之间数据转换
* String 为""的要转化成null
 */
public class BeanUtilExp
    extends BeanUtilsBean {

  public void copyValue(Object dest, Object orig) throws IllegalAccessException,
      InvocationTargetException {

    // Validate existence of the specified beans
    if (dest == null) {
      throw new IllegalArgumentException
          ("No destination bean specified");
    }
    if (orig == null) {
      throw new IllegalArgumentException("No origin bean specified");
    }

    PropertyDescriptor origDescriptors[] =
        getPropertyUtils().getPropertyDescriptors(orig);
    String toValue = null; //要转化的值

    for (int i = 0; i < origDescriptors.length; i++) {
      String name = origDescriptors[i].getName();
      if ("class".equals(name)) {
        continue; // No point in trying to set an object's class
      }
      if (getPropertyUtils().isReadable(orig, name) &&
          getPropertyUtils().isWriteable(dest, name)) {
        try {
          Object value =
              getPropertyUtils().getSimpleProperty(orig, name);

          /**
           * 这里处理String="",但对应转化属性的值要为null
           */
          if (value == null) {
            setNull(dest, name);
          }
          else if ( (value instanceof java.lang.String)) {
            toValue = (String) value;
            if (toValue.equals("")) {
              setNull(dest, name);
            }
            else {
              copyProperty(dest, name, toValue.trim());
            }
          }
          else {
            copyProperty(dest, name, value);
          }

        }
        catch (NoSuchMethodException e) {
          ; // Should not happen
        }
      }
    }

  }
  /**
   * 把requext里面的值copy给一个对象
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   */
  public void copyValRquestToBean(Object dest,javax.servlet.http.HttpServletRequest request)throws IllegalAccessException,
  InvocationTargetException {

	    // Validate existence of the specified beans
	    if (dest == null) {
	      throw new IllegalArgumentException
	          ("copy对象没有实例化");
	    }
	    if(request==null){
	    	throw new IllegalArgumentException
	          ("request对象没有实例化");
	    }
	    PropertyDescriptor destDescriptors[] =
	        getPropertyUtils().getPropertyDescriptors(dest);//要转化的java bean的属性集合
	    for (int i = 0; i < destDescriptors.length; i++) {//得到dest对象的属性
		      String name = destDescriptors[i].getName();//属性名
		      if ("class".equals(name) ||getPropertyUtils().isReadable(dest, name)==false) {
		        continue; // No point in trying to set an object's class
		      }
		      String value=request.getParameter(name);
		      if (value == null) {//在request里面不存在,则不改变bean的值
		    	  continue; 
	          }
		      if (value.equals("")) {//如果是空字符串，则把以前的设置为null
	              setNull(dest, name);
	            }
	            else {
	              copyProperty(dest, name, value);
	            }
	    }
	 
  }
  
  
  /**
   * 把request里面的值copy个一个list对象
   * @param dest  list里面保存的对象，标准的javaBean的class对象
   * @param request
   * @param beanProName request的参数名和java Bean的属性都包含的名称
   * @return
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   */
  public  List copyValRequestToList(Class destCls,javax.servlet.http.HttpServletRequest request,String beanProName)throws Exception {
 	 List list=new Vector();
 	 String[] beanProName_val_sz=request.getParameterValues(beanProName);
 	 if(beanProName_val_sz==null)
 		 return list;
 	 
 	 for(int index=0;index<beanProName_val_sz.length;index++){
 		Object dest=destCls.newInstance();
 	    PropertyDescriptor destDescriptors[] =
	        getPropertyUtils().getPropertyDescriptors(dest);//要转化的java bean的属性集合

	    for (int i = 0; i < destDescriptors.length; i++) {//得到dest对象的属性
	      String name = destDescriptors[i].getName();//属性名
	      if ("class".equals(name)) {
	        continue; // No point in trying to set an object's class
	      }
	      if (getPropertyUtils().isReadable(dest, name) ) {
	    	  String value=null;
	    	  String[] sz=request.getParameterValues(name);//得到request对象的值
	    	  if(sz!=null){
	    		  value=sz[index];
	    	  }
	          /**
	           * 这里处理String="",但对应转化属性的值要为null
	           */
	          if (value == null) {//在request里面不存在,则不改变bean的值
	        	 
	          }
	          else  {
	            if (value.equals("")) {//如果是空字符串，则把以前的设置为null
	              setNull(dest, name);
	            }
	            else {
	              copyProperty(dest, name, value);
	            }
	          }
	      }
	    }
	    
	    list.add(dest);
 	 }
 	 
 	 return list;
 	 
 	 
  }
  

  /**
   * 把javaBean的属性设置为Null
   * @param obj Object  actionForm
   * propertyName:表示对象的属性名如:name
   */
  private static void setNull(Object obj, String propertyName) throws
      IllegalAccessException {
    try {

      propertyName = "set" + propertyName.substring(0, 1).toUpperCase() +
          propertyName.substring(1); ;
      java.beans.Statement st = null;
      st = new java.beans.Statement(obj, propertyName, new Object[] {null});
      st.execute();
    }
    catch (Exception e) {
      throw new IllegalAccessException("赋予值出现错误:" + e.toString());
    }
  }

}
